package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.intercepter.Cache;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@CrossOrigin

public class OrderController {

    @Reference
    private OrderService orderService;


    /**分页查询**/
    @Cache
    @GetMapping(path = "/findPage")
    public Result findPage(OrderQueryPageBean queryPageBean){
        try {
            PageResult pageResult = orderService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**新增预约**/
    @PostMapping(path = "/submit")
    public Result handleAdd(@RequestBody Order order) {
        try {
            order.setOrderType(Order.ORDERTYPE_TELEPHONE);
            return orderService.submit(order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }

    /**根据id查询预约信息**/
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") String id) {
        try {
            Order order = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }
    /**更新预约信息**/
    @PostMapping(path = "/update")
    public Result handleUpdate(@RequestBody Order order) {
        try {
            orderService.update(order);
            return new Result(true, MessageConstant.UPDATE_ORDER_INFO_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_ORDER_INFO_FAIL);
        }
    }

    /**根据id删除预约信息**/
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") String id) {
        try {
            //根据id删除预约信息
            orderService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ORDER_INFO_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_INFO_FAIL);
        }
    }

    /**根据id修改用户预约状态**/
    @GetMapping(path = "/arrivalEdit/{$event}/{id}")
    public Result arrivalEdit(@PathVariable("$event") int event, @PathVariable("id") String id){
        orderService.arrivalEdit(event,id);
        return new Result(true,MessageConstant.UPDATE_ORDER_STATUS_SUCCESS);
    }
}
